package net;

import action.GetNoNameList;
import java.net.URI;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;

public class Connect
{
  private static final String UserAgent = "\tMillion/101 (GT-I9100; GT-I9100; 2.3.4) samsung/GT-I9100/GT-I9100:2.3.4/GRJ22/eng.build.20120314.185218:eng/release-keys";
  private static final String Auth = "eWa25vrE";
  private static final String Key = "2DbcAh3G";
  private static DefaultHttpClient client;

  public Connect()
  {
    client = new DefaultHttpClient();
    HttpParams hp = client.getParams();
    hp.setParameter("http.socket.timeout", Integer.valueOf(30000));
    hp.setParameter("http.connection.timeout", Integer.valueOf(30000));
  }

  private static List<NameValuePair> requestProcess(List<NameValuePair> source) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
  {
    ArrayList result = new ArrayList();
    Iterator i = source.iterator();
    while (i.hasNext()) {
      NameValuePair n = (NameValuePair)i.next();
      result.add(new BasicNameValuePair(n.getName(), Crypto.Encrypt2Base64NoKey(n.getValue())));
    }
    return result;
  }

  public byte[] connectToServer(String url, List<NameValuePair> content) throws Exception
  {
    List post = content;
    if (!url.equals(GetNoNameList.URL_GET_NONAME)) {
      post = requestProcess(content);
    }
    HttpPost hp = new HttpPost(url);
    hp.setHeader("User-Agent", "\tMillion/101 (GT-I9100; GT-I9100; 2.3.4) samsung/GT-I9100/GT-I9100:2.3.4/GRJ22/eng.build.20120314.185218:eng/release-keys");
    hp.setHeader("Accept-Encoding", "gzip, deflate");
    hp.setEntity(new UrlEncodedFormEntity(post, "UTF-8"));

    AuthScope as = new AuthScope(hp.getURI().getHost(), hp.getURI().getPort());
    CredentialsProvider cp = client.getCredentialsProvider();
    UsernamePasswordCredentials upc = new UsernamePasswordCredentials("eWa25vrE", "2DbcAh3G");
    cp.setCredentials(as, upc);
    byte[] b = (byte[])client.execute(hp, new HttpResponseHandler());

    if (b != null) {
      if (url.contains("gp_verify_receipt?"))
      {
        return null;
      }
      try {
        return Crypto.DecryptNoKey(b);
      } catch (Exception ex) {
        throw ex;
      }
    }
    connectToServer(url, content);

    return null;
  }
}